import wx, sys
from com.nasgektw.cards.client.model.Configuration import Configuration
from com.nasgektw.cards.client.gui.ImagePanel import ImagePanel
from com.nasgektw.cards.client.gui.ListCtrl import AutoWidthListCtrl
class CardsListPanel(wx.Panel):
    def __init__(self, *args, **kwds):
        wx.Panel.__init__(self, *args, **kwds)
        self.workspaceContent = AutoWidthListCtrl(self)
        self.workspaceContent.InsertColumn(0, 'Title')
        self.workspaceContent.InsertColumn(1, 'Type')
        self.workspaceContent.InsertColumn(2, 'Filename')
        self.imagePanel = ImagePanel(self)
        self.__do_binding__()
        self.__do_layout__()
    def refreshCards(self):
        self.workspaceContent.DeleteAllItems()
        for card in Configuration.cards:
            index = self.workspaceContent.InsertStringItem(sys.maxint, card.title)
            self.workspaceContent.SetStringItem(index, 1, card.type)
            self.workspaceContent.SetStringItem(index, 2, card.type + '/' + card.name)
    def __on_select_item__(self, event):
        fileName = "tmp.jpg"
        Configuration.cards[event.GetIndex()].getImage().save(fileName,"JPEG",quality=100)
        self.imagePanel.setImage(fileName)
    def __do_binding__(self):
        self.workspaceContent.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.__on_select_item__)
    def __do_layout__(self):
        sizer = wx.GridBagSizer(5,5)
        sizer.Add(self.workspaceContent, (0,0), wx.DefaultSpan, wx.EXPAND|wx.ALL, 0)
        sizer.Add(self.imagePanel, (0,1), wx.DefaultSpan, wx.EXPAND|wx.ALL, 0)
        sizer.AddGrowableCol(0, 1)
        sizer.AddGrowableCol(1, 2)
        sizer.AddGrowableRow(0, 1)
        self.SetSizer(sizer)